/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Manager_log接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： Manager_logRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Text;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class Manager_logRepository:BaseRepository<Manager_log,Int32>, IManager_logRepository
    {
        public Manager_logRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }
        /// <summary>
        /// 根据用户名返回上一次登录记录
        /// </summary>
        public Models.Manager_log GetModel(string user_name, int top_num, string action_type)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                int rows = _dbConnection.RecordCount<Manager_log>("select * from [" + nameof(Manager_log) + "] where user_name='" + user_name + "'");
                if (top_num == 1)
                {
                    rows = 2;
                }
                if (rows > 1)
                {
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("select top 1 * from (select top " + top_num + " * from [" + nameof(Manager_log) + "]");
                    strSql.Append(" where user_name=@user_name and action_type=@action_type order by id desc) as T ");
                    strSql.Append(" order by id asc");
                    return _dbConnection.QueryFirstOrDefault<Models.Manager_log>(strSql.ToString(), new { user_name = user_name, action_type = action_type });
                }
                return null;
            }
        }
        public async Task<Models.Manager_log> GetModelAsync(string user_name, int top_num, string action_type)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                int rows = _dbConnection.RecordCount<Manager_log>("where user_name='" + user_name + "'");
                if (top_num == 1)
                {
                    rows = 2;
                }
                if (rows > 1)
                {
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("select top 1 * from (select top " + top_num + " * from [" + nameof(Manager_log) + "]");
                    strSql.Append(" where user_name=@user_name and action_type=@action_type order by id desc) as T ");
                    strSql.Append(" order by id asc");
                    return await _dbConnection.QueryFirstOrDefaultAsync<Models.Manager_log>(strSql.ToString(), new { user_name = user_name, action_type = action_type });
                }
                return null;
            }
        }
        public int GetCount(string conditions)
        {
            return RecordCount(conditions);
        }

        public int DeleteDay(int dayCount)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("delete from [" + nameof(Manager_log) + "]  where DATEDIFF(d, add_time, getdate())>" + dayCount);
            }
        }
    }
}